<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * function.framework_component.php
 *
 * This is  a replacement for the plugin system, allows you to manage various
 * site components such as headers, footers, widgets, etc..
 * To use components simply call them in your templates, for example:
 * {framework_component component=Header}
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   Ian Szewczyk <roamzero@gmail.com>
 * @package     Framework
 * @subpackage  Template
 * @filesource
 */

function smarty_function_framework_component($params, &$smarty)
{
	if (empty($params['component'])) {
		$smarty->trigger_error("assign: missing 'component' parameter");
		return;
	} else {
		$component = $params['component'];
	}

	$componentObject = Framework_Component::factory($component,$params);

	if (!PEAR::isError($componentObject)) {
		$result = $componentObject->run();
		if (!PEAR::isError($result)) {

			$data = $componentObject->getData();
			
			// Check to see if ocmponent has form objects
			// if so, render them to array and assign them
			if (count($componentObject->forms)){
				require_once "HTML/QuickForm/Renderer/ArraySmarty.php";
				$renderer = new HTML_QuickForm_Renderer_ArraySmarty($smarty);
				foreach ($componentObject->forms as $var => $val){
					$val->accept($renderer);
					$data[$var] = $renderer->toArray();
				}
			}
			
			if (!is_null($componentObject->tplFile)) {
				$smarty->assign_by_ref($component,$data);
				return $smarty->fetch('framework:'.$componentObject->folder.'+'.$componentObject->tplFile);
			} else {
				foreach ($data AS $item) {
					if (is_string($item)) {
						return $item;
					}
				}
			}
		} else {
			$smarty->trigger_error($result->getMessage());
		}
	} else {
		$smarty->trigger_error($componentObject->getMessage());
	}

}

?>
